const express = require("express");
var MongoClient = require("mongodb").MongoClient;
const router = express.Router();
const { ObjectId } = require("mongodb");
const config = require("../config.js");
const client = new MongoClient(config.url);
const dbName = config.dbName;
// *-------

// -------*

// 以下为示例，请根据实际需求修改
const audicollDataName = "auditrlists"; // 数据表名称
const descrDataName = "auditdescribs"; // 数据表名称

// get请求获取列表示例
router.get("/ceshi", async (req, res) => {
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(audicollDataName);

    let result = await auditrlists.find({}).toArray();
    setTimeout(() => {
      res.send({
        errmsg: "成功",
        result: true,
        list: result,
      });
    }, 1500);
  } catch (err) {
    res.send({
      msg: "失败",
      success: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

// post请求获取列表示例完整版
router.post("/cbb/audit/auditRecord/query.do", async (req, res) => {
  var obj = req.body;
  let page = parseInt(req.query.startPage) || 1;
  let limit = parseInt(req.query.limit) || 10;
  let skip = (page - 1) * limit;
  console.log(obj, "12222");
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(audicollDataName);
    let result = await auditrlists.find(obj).skip(skip).limit(limit).toArray();

    let total = await auditrlists.countDocuments();
    setTimeout(() => {
      res.send({
        msg: "成功",
        success: true,
        results: result,
        totalCount: total,
      });
    }, 1500);
  } catch (err) {
    res.send({
      msg: "失败",
      success: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

// post 单纯请求获取列表数据
router.post("/audit/auditRecord", async (req, res) => {
  var obj = req.body;
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(descrDataName);
    let result = await auditrlists.find({}).toArray();
    res.send({
      errmsg: "成功",
      result: true,
      list: result,
    });
  } catch (err) {
    res.send({
      errmsg: "失败",
      success: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

// 新增
router.post("/audit/auditAdd", async (req, res) => {
  var obj = req.body;
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(descrDataName);
    let result = await auditrlists.insertOne(obj);
    res.send({
      errmsg: "新增成功",
      result: true,
      list: result,
    });
  } catch (err) {
    res.send({
      errmsg: "失败",
      result: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

// 修改
router.post("/audit/auditEdit", async (req, res) => {
  var obj = req.body;
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(descrDataName);
    const updateData = { ...obj };
    delete updateData._id;
    let result = await auditrlists.updateOne(
      { _id: new ObjectId(obj._id) },
      { $set: updateData }
    );
    res.send({
      errmsg: "修改成功",
      result: true,
      list: result,
    });
  } catch (err) {
    res.send({
      errmsg: "失败",
      result: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

// 删除
router.post("/audit/auditDel", async (req, res) => {
  var obj = req.body;
  try {
    await client.connect();
    const auditrlists = client.db(dbName).collection(descrDataName);
    let result = await auditrlists.deleteOne({
      _id: new ObjectId(obj._id),
    });
    res.send({
      errmsg: "删除成功",
      result: true,
      list: result,
    });
  } catch (err) {
    res.send({
      errmsg: "失败",
      result: false,
    });
  } finally {
    if (client) {
      await client.close(); // 确保无论成功还是失败都关闭连接
    }
  }
});

module.exports = router;
